//
//  CommentCell.m
//  ProjectsOne
//
//  Created by iOS开发 on 2018/7/10.
//  Copyright © 2018年 iOS开发. All rights reserved.
//

#import "CommentCell.h"
#import "CustomLayout.h"

@implementation StarView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        
        NSMutableArray *imageViewArr = [NSMutableArray array];
        
        for (NSInteger i = 0; i < 5; i++) {
            UIImageView *imageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"comment_star_yellow"]];
            [self addSubview:imageView];
            imageView.tag = 1000 + i;
            [imageViewArr addObject:imageView];
        }
        
        [imageViewArr mas_distributeViewsAlongAxis:MASAxisTypeHorizontal  withFixedSpacing:4 leadSpacing:0 tailSpacing:0];
        [imageViewArr mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self);
        }];
    }
    return self;
}

- (void)haveStarNumber:(NSInteger)number {
    NSInteger hiddenStarNumber = 5 - number;
    for (NSInteger i = 0; i < hiddenStarNumber; i++) {
        UIImageView *imageView = (UIImageView *)[self viewWithTag:1000 + i];
        imageView.hidden = YES;
    }
}

@end

@interface CommentCell()<UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout>

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIView *hiddenBottomView;
@property (nonatomic, strong) UICollectionView *collection;

@property (nonatomic, strong) UIView *bottomLine;
@property (nonatomic, strong) UILabel *nameLabel;
@property (nonatomic, strong) UIImageView *headImageView;
@property (nonatomic, strong) UILabel *timeLabel;
@property (nonatomic, strong) UILabel *contentLabel;
@property (nonatomic, strong) StarView *starView;

@property (nonatomic, assign) NSInteger picNumber;

@end

@implementation CommentCell

- (UICollectionView *)collection {
    if (!_collection) {
        CustomLayout *flowLayout = [[CustomLayout alloc] init];
        flowLayout.itemSize = CGSizeMake(kCellSize, kCellSize);
        flowLayout.minimumLineSpacing = 0;
        flowLayout.minimumInteritemSpacing = 20;
        flowLayout.maximumSpacing = 10;
        flowLayout.sectionInset = UIEdgeInsetsMake(13, 60, 20, 15);
        
        _collection = [[UICollectionView alloc] initWithFrame:CGRectZero collectionViewLayout:flowLayout];
        _collection.delegate = self;
        _collection.dataSource = self;
        _collection.scrollEnabled = NO;
        _collection.showsVerticalScrollIndicator = NO;
        _collection.showsHorizontalScrollIndicator = NO;
        _collection.pagingEnabled = YES;
        _collection.backgroundColor = [UIColor whiteColor];
        [self addSubview:_collection];
        
        [_collection registerClass:[UICollectionViewCell class] forCellWithReuseIdentifier:@"picCell"];
    }
    return _collection;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self createView];
    }
    return self;
}

- (void)createView {
    _bgView = [[UIView alloc] init];
    _bgView.layer.cornerRadius = 5;
    _bgView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_bgView];
    
    [_bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self).insets(UIEdgeInsetsMake(0, 10, 0, 10));
    }];
    
    UIView *hiddenTopView = [[UIView alloc] init];
    hiddenTopView.backgroundColor = [UIColor whiteColor];
    [self addSubview:hiddenTopView];
    
    [hiddenTopView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.mas_equalTo(self.bgView);
        make.height.mas_equalTo(10);
    }];
    
    _hiddenBottomView = [[UIView alloc] init];
    _hiddenBottomView.hidden = YES;
    _hiddenBottomView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_hiddenBottomView];
    
    [_hiddenBottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self.bgView);
        make.height.mas_equalTo(10);
    }];
    
    _bottomLine = [[UIView alloc] init];
    _bottomLine.backgroundColor = [UIColor colorWithHexString:@"dddddd"];
    _bottomLine.hidden = YES;
    [self addSubview:_bottomLine];
    
    [_bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.right.left.mas_equalTo(self);
        make.height.mas_equalTo(.5f);
    }];
    
    _headImageView = [[UIImageView alloc] init];
    _headImageView.backgroundColor = [UIColor redColor];
    _headImageView.layer.cornerRadius = 35 / 2;
    [self addSubview:_headImageView];

    [_headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.mas_equalTo(35);
        make.left.mas_equalTo(self.bgView).with.offset(15);
        make.top.mas_equalTo(self.bgView).with.offset(15);
    }];
    
    _nameLabel = [[UILabel alloc] init];
    _nameLabel.font = [UIFont systemFontOfSize:16];
    _nameLabel.textColor = [UIColor colorWithHexString:@"#333333"];
    [self addSubview:_nameLabel];
    
    [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.headImageView);
        make.left.mas_equalTo(self.headImageView.mas_right).with.offset(10);
        make.right.mas_lessThanOrEqualTo(self.bgView).with.offset(-100);
    }];
    
    _starView = [[StarView alloc] init];
    _starView.backgroundColor = [UIColor whiteColor];
    [self addSubview:_starView];
    
    [_starView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.nameLabel);
        make.right.mas_equalTo(self.bgView).with.offset(-15);
        make.height.mas_equalTo(30);
    }];
    
    _timeLabel = [[UILabel alloc] init];
    _timeLabel.font = [UIFont systemFontOfSize:13];
    _timeLabel.textColor = [UIColor colorWithHexString:@"999999"];
    [self addSubview:_timeLabel];

    [_timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(self.bgView).with.offset(-15);
        make.left.mas_equalTo(self.nameLabel);
        make.right.mas_lessThanOrEqualTo(self.bgView).with.offset(-10);
    }];
    
    _contentLabel = [[UILabel alloc] init];
    _contentLabel.font = [UIFont systemFontOfSize:16];
    _contentLabel.textColor = [UIColor colorWithHexString:@"666666"];
    _contentLabel.numberOfLines = 0;
    [self addSubview:_contentLabel];
    
    [_contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.nameLabel);
        make.right.mas_equalTo(self.bgView).with.offset(-15);
        make.top.mas_equalTo(self.nameLabel.mas_bottom).with.offset(20);
    }];
    
    [self.collection mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.left.mas_equalTo(self.bgView);
        make.top.mas_equalTo(self.contentLabel.mas_bottom);
        make.bottom.mas_equalTo(self.timeLabel.mas_top);
    }];
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _picNumber;
}

- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    UICollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"picCell" forIndexPath:indexPath];
    
    cell.backgroundColor = [UIColor yellowColor];
    cell.layer.cornerRadius = 2;
    
    return cell;
}

- (void)updateUIFromData:(commentModel *)model {
    _nameLabel.text = [NSString stringWithFormat:@"%@", model.name];
    _timeLabel.text = [NSString stringWithFormat:@"%@", model.time];
    _contentLabel.attributedText = model.attrComment;
    _picNumber = model.pictureNumber;
    [_starView haveStarNumber:model.starNumber];
}

- (void)CellForEndCell:(BOOL)isEnd {
    _bottomLine.hidden = isEnd;
    _hiddenBottomView.hidden = isEnd;

}

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
